import store from "../store";
import _random from "lodash/random";
import _isEmpty from "lodash/isEmpty";
import _range from "lodash/range";
// 随机下注
export const randomChip = (nums, cb) => {
  let i = 0;
  while (i < nums) {
    let data = randomData();
    store.commit("set_cart_list", data);
    i++;
  }
  cb && cb();
};
// 随机1注
/**
 *  单式 1 复式 0
 */
const playTypeArr = {
  '五星直选复式': {type: 0, length: 5},
  '五星直选组合': {type: 0, length: 5},
  '五星直选单式': {type: 1, length: 5},
  '趣味一帆风顺': {type: 0, length: 1},
  '趣味好事成双': {type: 0, length: 1},
  '趣味三星报喜': {type: 0, length: 1},
  '趣味四季发财': {type: 0, length: 1},
  '前三直选复式': {type: 0, length: 3},
  '前三直选单式': {type: 1, length: 3},
  '中三直选复式': {type: 0, length: 3},
  '中三直选单式': {type: 1, length: 3},
  '后三直选复式': {type: 0, length: 3},
  '后三直选单式': {type: 1, length: 3},
}
export const randomData = () => {
  let { multiple, amount_model, current_play_type } = store.state;
  let temp = [];
  while (temp.length < playTypeArr[current_play_type].length) {
    temp.push(_random(9));
  }
  let result = {
    num_str: playTypeArr[current_play_type].type === 0? temp.join("|") : temp.join(","),
    multiple,
    amount_model,
    playType: current_play_type,
    numbers: 1
  };
  return result;
};

// 过滤输入内容
// params str 输入string n 间隔
// return saveRepeat 数组重复元素,saveDel 删除字符, emptyStr: 是否有重复号, result 格式化后val
export const handlerFilterTextarea = (str, n) => {
  let saveDel = ""; //保留删除字符
  let saveRepeat = []; //保留数组重复元素
  let emptyStr = ""; //空白占位符
  let repeatArr = []; //重复的元素array
  let result = [];
  str = str.replace(/\,+/g, "");
  if (str.length % n !== 0) {
    saveDel = str.slice(-(str.length % n));
    str = str.slice(0, str.length - (str.length % n));
  }
  if(formatToThousands(str, n).length){
    result = formatToThousands(str, n).split(",");
    repeatArr = result.filter(i => result.indexOf(i) !== result.lastIndexOf(i));
    if (!_isEmpty(repeatArr)) {
      saveRepeat = [...new Set(repeatArr)];
      emptyStr = " ";
    }
  }
  return {
    saveRepeat,
    saveDel,
    emptyStr,
    result: [...new Set(result)]
  };
};
//每隔n位小数加逗号
export const formatToThousands = (num, n) => {
  var num = num.toString();
  var result = "";
  while (num.length > n) {
    result = "," + num.slice(-n) + result;
    num = num.slice(0, num.length - n);
  }
  if (num) {
    result = num + result;
  }
  return result;
};

// 前三直选和值
export const getSumArrayTotalNum = (arr) => {
  if(_isEmpty(arr)) return 0;
  return arr.reduce((total,item) => total + getSumValueTotalNum(item) ,0)
}

// 获取单个和值总数
export const getSumValueTotalNum = (sum) => {
  let num = 0;
  let one = _range(10);
  let two = _range(10);
  let three = _range(10);
  for (let i=0; i < one.length; i++) {
    for (let j=0; j < two.length; j++) {
      for (let k=0; k < three.length; k++) {
        if(one[i] + two[j] + three[k] == sum){
          num++;
        }
      }
    }
  }
  return num;
}


export function countDown(last_time,callback){
	//定义当前时间
	var startTime = new Date();
	//定义结束时间
	var endTime = new Date(last_time);

	var countDown = (endTime.getTime() - startTime.getTime())/1000;

	// var oDay = parseInt(countDown/(24*60*60));
	var oHours = size(parseInt(countDown/(60*60)));
	var oMinutes = size(parseInt(countDown/60%60));
	var oSeconds = size(parseInt(countDown%60));
	if(countDown < 0){
		callback();
	}else{
		return `${oHours}:${oMinutes}:${oSeconds}`
	}
}

function size(d){
	return d < 10 ? '0' + d : d;
}

export function isPhone(phone) {
  return /^1[3456789]\d{9}$/.test(phone)
}
